from . import db
from flask_login import UserMixin
from datetime import datetime

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password_hash = db.Column(db.String(128))
    is_admin = db.Column(db.Boolean, default=False)
    comments = db.relationship('Comment', backref='author', lazy='dynamic')
    votes = db.relationship('Vote', backref='user', lazy='dynamic')
    comment_likes = db.relationship('CommentLike', backref='user', lazy='dynamic')

class Couple(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    season = db.Column(db.Integer, nullable=False)
    
    # 丈夫信息
    husband_name = db.Column(db.String(64), nullable=False)
    husband_age = db.Column(db.Integer)
    husband_occupation = db.Column(db.String(64))
    husband_avatar = db.Column(db.String(256))
    
    # 妻子信息
    wife_name = db.Column(db.String(64), nullable=False)
    wife_age = db.Column(db.Integer)
    wife_occupation = db.Column(db.String(64))
    wife_avatar = db.Column(db.String(256))
    
    # 夫妻共同信息
    story = db.Column(db.Text)
    marriage_years = db.Column(db.Integer)
    likes_count = db.Column(db.Integer, default=0)
    
    comments = db.relationship('Comment', backref='couple', lazy='dynamic')
    votes = db.relationship('Vote', backref='couple', lazy='dynamic')

class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text, nullable=False)
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)
    likes_count = db.Column(db.Integer, default=0)
    is_hidden = db.Column(db.Boolean, default=False)
    
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    couple_id = db.Column(db.Integer, db.ForeignKey('couple.id'))
    
    # 评论的回复
    parent_id = db.Column(db.Integer, db.ForeignKey('comment.id'))
    replies = db.relationship('Comment', backref=db.backref('parent', remote_side=[id]))
    
    # 评论点赞
    liked_by = db.relationship('CommentLike', backref='comment', lazy='dynamic')
    
    def is_liked_by(self, user):
        return CommentLike.query.filter_by(user_id=user.id, comment_id=self.id).first() is not None

class Vote(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    vote_type = db.Column(db.String(20))  # 'divorce' or 'stay_together'
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)
    
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    couple_id = db.Column(db.Integer, db.ForeignKey('couple.id'))
    
    # 添加唯一约束，确保每个用户只能对每对夫妻投票一次
    __table_args__ = (db.UniqueConstraint('user_id', 'couple_id', name='unique_user_couple_vote'),)

class Quote(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text, nullable=False)
    author = db.Column(db.String(64))
    is_active = db.Column(db.Boolean, default=True)

class CommentLike(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)
    
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    comment_id = db.Column(db.Integer, db.ForeignKey('comment.id'))
    
    # 添加唯一约束，确保每个用户只能给每条评论点赞一次
    __table_args__ = (db.UniqueConstraint('user_id', 'comment_id', name='unique_user_comment_like'),)